﻿using Infraestructure.Data.MainModule.Repositories;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using Infraestructure.Data.MainModule.UnitOfWork;
using Infraestructure.CrossCutting.Logging;
using Domain.MainModule.Entities;
using Domain.Core.Specification;
using System.Collections.Generic;
using Domain.MainModule.SoundexCodes;

namespace Infraestructure.Data.MainModule.Tests
{
    /// <summary>
    ///This is a test class for SoundexCodeRepositoryTest and is intended
    ///to contain all SoundexCodeRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SoundexCodeRepositoryTest : RepositoryTestsBase<SoundexCode>
    {
        private TestContext testContextInstance;
        private string soundexCodeValue = "Soundex Code";

        public override System.Linq.Expressions.Expression<Func<SoundexCode, bool>> FilterExpression
        {
            get { return a => a.UserId == TestConstants.Instance.USER_ID; }
        }

        public override System.Linq.Expressions.Expression<Func<SoundexCode, Guid>> OrderByExpression
        {
            get { return a => a.UserId; }
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindSoundexCode_Invoke_NullSpecThrowNewArgumentNullException_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ISoundexCodeRepository repository = new SoundexCodeRepository(context, traceManager);

            //Act
            repository.FindSoundexCode(null);
        }

        #region SoundexCodeUserIdSpecification

        [TestMethod()]
        public void FindSoundexCodeByUserId_Invoke_InvalidUserIdReturnEmptyCollection_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ISoundexCodeRepository repository = new SoundexCodeRepository(context, traceManager);

            Guid invalidId = new Guid("11111111-1111-1111-1111-111111111111");
            SoundexCodeUserIdSpecification spec = new SoundexCodeUserIdSpecification(invalidId);

            //Act
            IEnumerable<SoundexCode> soundexCodes = repository.FindSoundexCode(spec);

            //Assert
            Assert.IsNotNull(soundexCodes);
            Assert.AreEqual(0, soundexCodes.Count());
        }

        [TestMethod()]
        public void FindSoundexCodeByUserId_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ISoundexCodeRepository repository = new SoundexCodeRepository(context, traceManager);

            SoundexCodeUserIdSpecification spec = new SoundexCodeUserIdSpecification(TestConstants.Instance.USER_ID);

            //Act
            IEnumerable<SoundexCode> soundexCodes = repository.FindSoundexCode(spec);

            //Assert
            Assert.IsNotNull(soundexCodes);
            Assert.AreEqual(1, soundexCodes.Count());
        }
        
        #endregion

        #region SoundexCodeValueSpecification

        [TestMethod()]
        public void FindSoundexCodeByValue_Invoke_InvalidValueReturnEmptyCollection_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ISoundexCodeRepository repository = new SoundexCodeRepository(context, traceManager);

            SoundexCodeValueSpecification spec = new SoundexCodeValueSpecification("invalid soundex code");

            //Act
            IEnumerable<SoundexCode> soundexCodes = repository.FindSoundexCode(spec);

            //Assert
            Assert.IsNotNull(soundexCodes);
            Assert.AreEqual(0, soundexCodes.Count());
        }

        [TestMethod()]
        public void FindSoundexCodeByValue_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            ISoundexCodeRepository repository = new SoundexCodeRepository(context, traceManager);

            SoundexCodeValueSpecification spec = new SoundexCodeValueSpecification(soundexCodeValue);

            //Act
            IEnumerable<SoundexCode> soundexCodes = repository.FindSoundexCode(spec);

            //Assert
            Assert.IsNotNull(soundexCodes);
            Assert.AreEqual(1, soundexCodes.Count());
        }
        
        #endregion
    }
}
